package com.example.mystoryapp2.ui.story.storylist

import com.example.mystoryapp2.adapter.StoryListAdapter
import com.example.mystoryapp2.contexts.AppContext
import com.example.mystoryapp2.contexts.StoryContext
import com.example.mystoryapp2.data.network.ServiceCreator
import com.example.mystoryapp2.data.network.model.StoryModel
import com.example.mystoryapp2.domain.Story
import com.example.mystoryapp2.enums.EnumListMode
import com.example.mystoryapp2.ui.base.BaseListAdapter
import com.example.mystoryapp2.ui.base.BaseViewModel

open class StoryListViewModel : BaseViewModel() {

    var adapter = StoryListAdapter(ArrayList())

    fun initStoryList(listener: BaseListAdapter.IClickListener<Story>) {
        val callBack = object : ServiceCreator.ICallBack<List<Story>> {
            override fun onSuccess(res: List<Story>) {
                adapter.setList(res as ArrayList<Story>)
                adapter.setClickListener(listener)
            }
        }
        if (StoryContext.listMode == EnumListMode.WORK) {
            AppContext.userInfo.userId?.let {
                StoryModel.getPublishedStory(it, callBack, StoryContext.storyIsPublished)
            }
        } else {
            AppContext.userInfo.userId?.let {
                StoryModel.getCollectedStory(it, callBack)
            }
        }

    }
}